//
//  MyBillViewController.swift
//  cxd4iphone
//
//  Created by hexy on 12/3/15.
//  Copyright © 2015 hexy. All rights reserved.
//

import UIKit

class MyBillViewController: BaseTableViewController {

    private lazy var myBillListViewModel = MyBillListViewModel()
    
    private lazy var hideSectionHeader: Bool = true
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        loadData(true)
    }

    private func loadData(pullDown: Bool) {
        
        xx_showHUD(nil)
        myBillListViewModel.loadUserBillList(pullDown).subscribeError({ (error) -> Void in
            
                xx_showError(error)
            
            }) { () -> Void in
                
                xx_hideHUD()
                self.tableView.reloadData()
                if self.myBillListViewModel.sections[0].count > 0 {
                    
                    self.hideSectionHeader = false
                    self.navigationItem.rightBarButtonItem = UIBarButtonItem(target: self, action: "showPickerView")
                } else {
                    
                    self.hideSectionHeader = true
                }
                
                
        }
        
    }
    override func makeTableView() {
        
        tableView.sectionHeaderHeight = xx_height(30)
 
        self.tableListViewModel = myBillListViewModel
        
        
        tableView.registerClass(MyBillListCell.self, forCellReuseIdentifier: MyBillCellReuseID)
        
    }
    
    func showPickerView() {
        
        let pick = PickerView()
        
        pick.show(myBillListViewModel.pickerViewModel) { (rows) -> Void in
            
            let pvm = self.myBillListViewModel.pickerViewModel
            
            let status = (pvm.components[0][rows[0]]).status
            let dmax = (pvm.components[1][rows[1]]).max
            let dmin = (pvm.components[1][rows[1]]).min
            
            let qm = self.myBillListViewModel.queryModel
            
            qm.status = status
            qm.dateMax = dmax
            qm.dateMin = dmin
 
            self.loadData(true)
        }
    }
    var stateCache: [NSIndexPath :Bool] = [:]
}

extension MyBillViewController {
    
    
    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        return hideSectionHeader ? UIView() : MyBillSectionHeaderView()
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        let isOpen = stateCache[indexPath] ?? false

        stateCache[indexPath] = !isOpen
        tableView.beginUpdates()
        tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
        tableView.endUpdates()
        
        
        let cell = tableView.cellForRowAtIndexPath(indexPath) as! MyBillListCell
        
        cell.indecatorBtn.imageView?.transform = (isOpen) ? CGAffineTransformIdentity : CGAffineTransformMakeRotation(CGFloat(M_PI))
        
        
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
//        if let state = stateCache[indexPath] where state {
        
            let viewModel = myBillListViewModel.sections[indexPath.section][indexPath.row] as! MyBillCellViewModel
            
            if viewModel.rowHeight > 0 {
                return viewModel.rowHeight
            }
            
            let cell = tableView.dequeueReusableCellWithIdentifier(viewModel.reuseId) as! MyBillListCell
            
            viewModel.rowHeight = cell.rowHeight(viewModel)
            
            return viewModel.rowHeight
//        }
//        return xx_height(38)
    }

    
}
